$(function() {
    $('input[name="username"]').focus();
    // 密码显示与隐藏
    let pw_eye = document.getElementById('showText');
    let pw_input = document.getElementById('password');
    pw_eye.onclick = function () {
        if (pw_input.type == 'password') {
            pw_input.type = 'text';
            pw_eye.src = 'static/icon/password_visible.png';
        } else {
            pw_input.type = 'password';
            pw_eye.src = 'static/icon/password_hide.png';
        }
    }
    // 点击按钮登录
    $('button[type="submit"]').on('click', function () {
        loginFun();
    });
    // enter键登录
    $(document).keydown(function (ev) {
        if (ev.keyCode == 13) {
            loginFun();
        }
    });
	
	function setCookie(cname, cvalue, exdays) {
	  var d = new Date();
	  d.setTime(d.getTime() + (exdays *60*60*1000));
	  var expires = "expires=" + d.toGMTString();
	  document.cookie = cname + "=" + cvalue + "; " + expires;
	 }
	
	 function getCookie(cname) {
	  var name = cname + "=";
	  var ca = document.cookie.split(';');
	  for(var i = 0; i < ca.length; i++) {
	   var c = ca[i].trim();
	   if(c.indexOf(name) == 0) {
	    return c.substring(name.length, c.length);
	   }
	  }
	  return "";
	 }
	
    function loginFun() {
        let pattern = new RegExp("[`~!@#$^&*=|{}':;',\\[\\]<>《》/?~！@#￥……&*|{}【】‘；：”“'。，、？' ']");
        let userNameVal = $('input[name="username"]').val();
        let passwordVal = $('input[name="password"]').val();
        let is_usernull = pattern.test(userNameVal);
        let is_passnull = pattern.test(passwordVal);
        //前台表单验证
        if (is_usernull) {
            $('.errorTip').css('display', 'block');
            $('.errorTip span').text('用户名格式不正确');
            return;
        }
        if (userNameVal == '') {
            $('.errorTip').css('display', 'block');
            $('.errorTip span').text('用户名不能为空');
            return;
        }
        if (userNameVal.length < 5) {
            $('.errorTip').css('display', 'block');
            $('.errorTip span').text('用户名长度大于5位字符');
            return;
        }
        if (is_passnull) {
            $('.errorTip').css('display', 'block');
            $('.errorTip span').text('密码格式不正确');
            return;
        }
        if (passwordVal == '') {
            $('.errorTip').css('display', 'block');
            $('.errorTip span').text('密码不能为空');
            return;
        }
        // if (passwordVal.length < 5) {
            // $('.errorTip').css('display', 'block');
            // $('.errorTip span').text('密码长度为5位字符');
            // return;
        // }
		_login();
    };
	function _login() {
	    $.post('/jg-api/login', {
	        userName: $('#username').val(),
	        password: $('#password').val()
	    }, function (data) {
	        if (data.success) {
				setCookie('user',username,1);
	            location.href = 'index.html?'+$('#username').val();
				console.log(data.message);
	        } else {
	            $('.errorTip').css('display', 'block');
				$('.errorTip span').text(data.message);
				setTimeout(()=>{
					$('.errorTip').css('display', 'none');
				},1500)
	        }
	    });
		
		
	};
});